package com.geetour.columbus.region.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.geetour.columbus.common.model.R.CityPage;
import com.geetour.columbus.common.model.R.ProvincePage;
import com.geetour.columbus.common.service.ICityPageService;
import com.geetour.columbus.common.service.IProvincePageService;
import com.geetour.columbus.common.utils.StringUtil;

@Controller
public class CityPageController {

    private IProvincePageService provincePageServiceImpl;
    private ICityPageService     cityPageServiceImpl;

    @Autowired
    public void setProvincePageServiceImpl(IProvincePageService provincePageServiceImpl) {
        this.provincePageServiceImpl = provincePageServiceImpl;
    }

    @Autowired
    public void setCityPageServiceImpl(ICityPageService cityPageServiceImpl) {
        this.cityPageServiceImpl = cityPageServiceImpl;
    }

    @RequestMapping("/region/cityPageQuery.jsp")
    public String query(ModelMap modelMap, CityPage city) {
        if (city != null) {
            List<CityPage> cityPageList = cityPageServiceImpl.findCityPages(city);
            modelMap.put("cityPageList", cityPageList);
            List<ProvincePage> provincePageList = provincePageServiceImpl.findAllProvincePages();
            modelMap.put("provincePageList", provincePageList);
        }
        return "/region/cityPageList.html";
    }

    @RequestMapping("/region/cityPageAddInit.jsp")
    public String addInit(ModelMap modelMap) {
        try {
            List<ProvincePage> provincePageList = provincePageServiceImpl.findAllProvincePages();
            modelMap.put("provincePageList", provincePageList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "/region/cityAdd.html";
    }

    @RequestMapping("/region/cityPageAdd.jsp")
    public String addBatch(ModelMap modelMap, String province, String citys) {
        List<String> list = StringUtil.lineInput(citys);
        if (province == null || list == null || list.isEmpty()) {
            return "/region/cityPageAdd.html";
        }
        int result = cityPageServiceImpl.insertCityPageNames(province, list);
        List<ProvincePage> provincePageList = provincePageServiceImpl.findAllProvincePages();
        modelMap.put("result", result);
        modelMap.put("province", province);
        modelMap.put("provincePageList", provincePageList);
        return "/region/cityPageAdd.html";
    }
    
    @RequestMapping("/region/cityPageDelete.jsp")
    public String delete(ModelMap modelMap, CityPage city) {
        cityPageServiceImpl.deleteCityPageById(city.getId());
        city.setId(null);
        return this.query(modelMap, city);
    }
}
